package com.hsd.controller;

import com.hsd.service.IAccountService;
import com.hsd.service.ILogService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

@Controller
public class ATMController {
    @Resource
    private IAccountService accountService;
    @Resource
    private ILogService logService;

    /**
     * 初始化页面显示
     *
     * @param map
     * @return
     */
    @RequestMapping("/index")
    public String init(ModelMap map) {
        map.put("accounts", accountService.findAll());
        return "/index.jsp";
    }

    /**
     * 存钱操作
     *
     * @param map
     * @return
     */
    @RequestMapping("/acc/save")
    public String saveMoney(ModelMap map, String acc, Double money) {
        map.put("os", accountService.save(acc, money));
        return "/index.do";
    }

    /**
     * 取钱操作
     *
     * @param map
     * @return
     */
    @RequestMapping("/acc/take")
    public String takeMoney(ModelMap map, String acc, Double money) {
        map.put("os", accountService.take(acc, money));
        return "/index.do";
    }

    /**
     * 转账操作
     *
     * @param map
     * @return
     */
    @RequestMapping("/acc/transfer")
    public String transferMoney(ModelMap map, String from, String to, Double money) {
        map.put("os", accountService.transfer(from, to, money));
        return "/index.do";
    }

    /**
     * 转账操作
     *
     * @param map
     * @return
     */
    @RequestMapping("/acc/logs")
    public String findAccountLogs(ModelMap map, String acc) {
        map.put("logs", logService.findByAcc(acc));
        map.put("acc", acc);
        return "/index.do";
    }
}
